package com.martin.lib_base.model

import android.app.DatePickerDialog
import android.content.Context
import androidx.compose.runtime.mutableStateOf
import com.martin.lib_base.R
import dev.utils.DevFinal
import dev.utils.common.DateUtils
import java.util.Calendar

class DateModel() {

    // 日期
    val date = mutableStateOf("")

    /**
     * 获取当前日期
     * 如果为空,则返回当前日期
     */
    fun getCalendar(): Calendar {
        return if (date.value.isEmpty()) {
            DateUtils.getCalendar()
        } else {
            DateUtils.getCalendar(date.value, DevFinal.TIME.yyyyMMdd_HYPHEN)
        }
    }

    /**
     * 选择日期
     */
    fun selectDate(context: Context) {
        val calendar = getCalendar()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        DatePickerDialog(
            context,
            { _, year, month, dayOfMonth ->
                calendar.set(year, month, dayOfMonth)
                date.value = DateUtils.formatDate(calendar.time, DevFinal.TIME.yyyyMMdd_HYPHEN)
            },
            year, month, day
        ).show()
    }
}